import java.util.HashSet;
import java.util.LinkedList;


public class aWords {
	public static void main(String[] args) {
		new aWords();
	}
	
	private HashSet<aNode> graph = new HashSet<aNode>();
	private final int words = 10;
	private LinkedList<aNode> queue = new LinkedList<aNode>();
	private int queueCount = 0;

	public aWords() {
		loadNodes();
		findEdges();
		System.out.println(graph);
		test();
	}
	
	/**
	 * Load the data
	 */
	private void loadNodes() {
		for(int i=0;i<words;i++) {
			String str = StdIn.readString();
			graph.add(new aNode(str));
		}
	}
	
	/**
	 * Set each node to find all its edges
	 */
	private void findEdges() {
		for(aNode node : graph) {
			node.findEdges(graph);
		}
	}
	
	/**
	 * Test with different node combinations
	 */
	private void test() {
		for(int i=0;i<6;i++) {
			String str1 = StdIn.readString();
			String str2 = StdIn.readString();
			aNode node1 = null;
			aNode node2 = null;
			
			for(aNode node : graph) {
				if (node.getWord().equals(str1)) node1 = node;
				if (node.getWord().equals(str2)) node2 = node;
			}
			
			assert node1 != null;
			assert node2 != null;
			
			StdOut.println(distance(node1,node2));
		}
	}
	
	/**
	 * Find the distance between 2 nodes
	 * @param node1
	 * @param node2
	 * @return
	 */
	private int distance(aNode node1, aNode node2) {
		
		startBfs(node1,node2);
		
		return -1;
		
	}
	
	/**
	 * BFS
	 * @param node
	 */
	private void bfs(aNode node,aNode goal,int depth) {
		
		
		if (node.getWord().equals(goal.getWord())) {
			queue.clear();
			System.out.println(depth);
			return;
		}
		
		depth++;
		
		for(aNode edge : node) {
			queue.add(edge);
		}
		if(!queue.isEmpty())
			bfs(queue.pop(),goal,depth);		
	}
	
	private void startBfs(aNode node, aNode goal) {
		queue.add(node);
		
		bfs(queue.pop(),goal,0);
	}
	
}
